package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 定时清理七牛云垃圾图片任务
 */
public class ClearImgJob {
    //注入jedis连接池
    @Autowired
    private JedisPool jedisPool;

    //定义清理图片的任务类
    public void clearImg() {
        //通过差集，获取垃圾图片的集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //判断
        if (set != null && set.size() > 0) {
            for (String fileName : set) {
                //如果存在差集，调用工具类删除七牛云中的垃圾图片
                QiniuUtils.deleteFileFromQiniu(fileName);
                //并且删除Redis中差集信息
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            }
        }
    }
}
